package com.farmer.product.controller;

import com.farmer.common.entity.Product;
import com.farmer.common.result.Result;
import com.farmer.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 农产品控制器
 */
@Slf4j
@RestController
@RequestMapping("/product")
@Tag(name = "农产品管理", description = "农产品相关接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 获取农产品列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取农产品列表", description = "分页获取农产品列表，支持分类和关键词搜索")
    public Result<?> getProductList(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(required = false) String category,
                                   @RequestParam(required = false) String keyword,
                                   @RequestParam(required = false) Integer status) {
        return productService.getProductList(pageNum, pageSize, category, keyword, status);
    }

    /**
     * 获取农产品详情
     */
    @GetMapping("/{productId}")
    @Operation(summary = "获取农产品详情", description = "根据商品ID获取农产品详细信息")
    public Result<?> getProductById(@PathVariable Long productId) {
        return productService.getProductById(productId);
    }

    /**
     * 添加农产品
     */
    @PostMapping("/add")
    @Operation(summary = "添加农产品", description = "添加新的农产品")
    public Result<?> addProduct(@Valid @RequestBody Product product) {
        log.info("添加农产品请求：{}", product.getName());
        return productService.addProduct(product);
    }

    /**
     * 更新农产品信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新农产品信息", description = "更新农产品详细信息")
    public Result<?> updateProduct(@Valid @RequestBody Product product) {
        log.info("更新农产品请求：{}", product.getName());
        return productService.updateProduct(product);
    }

    /**
     * 删除农产品
     */
    @DeleteMapping("/{productId}")
    @Operation(summary = "删除农产品", description = "根据商品ID删除农产品")
    public Result<?> deleteProduct(@PathVariable Long productId) {
        log.info("删除农产品请求，商品ID：{}", productId);
        return productService.deleteProduct(productId);
    }

    /**
     * 更新商品状态
     */
    @PutMapping("/status/{productId}")
    @Operation(summary = "更新商品状态", description = "上架或下架商品")
    public Result<?> updateProductStatus(@PathVariable Long productId, @RequestParam Integer status) {
        log.info("更新商品状态请求，商品ID：{}，状态：{}", productId, status);
        return productService.updateProductStatus(productId, status);
    }

    /**
     * 获取商品分类列表
     */
    @GetMapping("/categories")
    @Operation(summary = "获取商品分类列表", description = "获取所有商品分类")
    public Result<?> getCategoryList() {
        return productService.getCategoryList();
    }

    /**
     * 更新商品库存
     */
    @PutMapping("/stock/{productId}")
    @Operation(summary = "更新商品库存", description = "增加或减少商品库存")
    public Result<?> updateStock(@PathVariable Long productId, @RequestParam Integer quantity) {
        log.info("更新库存请求，商品ID：{}，数量：{}", productId, quantity);
        return productService.updateStock(productId, quantity);
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查服务是否正常运行")
    public Result<?> health() {
        return Result.success("农产品服务运行正常");
    }
} 